package com.wanxi.service.impl;

import com.wanxi.dao.impl.UserDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.MD5;
import com.wanxi.vo.UserVO;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDaoImpl userDao = new UserDaoImpl();

    public ResultDTO add(UserVO userVO) {

//        String mingwen = model.getPassword();
//        String anwen = MD5.encoderByMd5(mingwen);
//        model.setPassword(anwen);
//        model.setPassword(MD5.encode(model.getPassword()));
//        if (model.getBirthday() == null) {
//            model.setBirthday("1900-01-01");
//        }
//        model.setBirthday(Tool.strToDate(model.getBirthday()));
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        return ResultDTO.builder()
                .count(userDao.add(model))
                .build();
    }

    public ResultDTO delete(UserVO userVO) {
//        UserDao userDao = new UserDao();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        return ResultDTO.builder()
                .count(userDao.delete(model))
                .build();
    }

    public ResultDTO update(UserVO userVO) {
//        UserDao userDao = new UserDao();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        return ResultDTO.builder()
                .count(userDao.update(model))
                .build();
    }

//    public List<UserModel> findAll(UserVO userVO) {
//        UserDao userDao = new UserDao();
//        return userDao.findAll(model);
//    }

    public ResultDTO findAll(UserVO userVO) {
//        UserDao userDao = new UserDao();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        List<UserModel> modelList = userDao.findAll(model);
        int count = userDao.count(model);
        ResultDTO resultModel = ResultDTO.builder()
                .data(modelList)
                .count(count)
                .code(0)
                .build();
        return resultModel;
    }


    public ResultDTO count(UserVO userVO) {
//        UserDao userDao = new UserDao();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        return ResultDTO.builder()
                .count(userDao.count(model))
                .build();
    }

    public ResultDTO findById(UserVO userVO) {
//        UserDao userDao = new UserDao();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        return ResultDTO.builder()
                .data(userDao.findById(model))
                .build();
    }

    /**
     * 登录：
     * 第一：验证码是否正确
     * 2.密码应该先加密
     * 3.讲vo的数据封装到model
     * 4.根据用户名和密码到数据库里面查询，并返回结果
     * 5.判断是否登录成功
     *
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO login(UserVO userVO) {
        if (!userVO.getCode().equals(userVO.getVCode())) {
//            在数据库里面查询,根据用户名和密码查询，所以应该返回一个对象
            return ResultDTO.builder()
                    .code(-1)
                    .msg("error-code")
                    .build();
        }
        userVO.setPassword(MD5.encoderByMd5(userVO.getPassword()));
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
        UserModel model = userDao.findByUsernameAndPassword(userModel);
        if (model == null) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("error")
                    .build();
        }
        return ResultDTO.builder()
                .code(1)
                .msg("success")
                .build();

    }
}
